package android.slc.appbase.ui.vd

import android.slc.appbase.ui.AppView
import android.slc.box.core.property.DataChangeListener
import android.slc.rx.SimpleSingleObserver
import android.slc.rx.SlcRxJavaUtils
import io.reactivex.rxjava3.core.Single
import javafx.application.Platform
import javafx.scene.control.ListCell
import javafx.scene.control.ListView
import javafx.scene.control.skin.VirtualFlow
import javafx.scene.input.ScrollEvent
import javafx.scene.layout.Region
import java.io.Serializable
import java.util.concurrent.TimeUnit

abstract class BaseListPageViewVd<T : Serializable, VB : BaseListPageVmBox<T>> : BaseListPageVd<T, VB>() {

    protected lateinit var listView: ListView<T>
        private set
    protected lateinit var virtualFlow: VirtualFlow<*>
        private set
    protected lateinit var appView: AppView<*>
        private set

    fun init(contentView: Region?, vmBox: VB?, appView: AppView<*>) {
        this.appView = appView
        super.init(contentView, vmBox)
    }

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        this.listView = this.contentView.lookup(getListViewId()) as ListView<T>
        Platform.runLater {
            this.virtualFlow = this.listView.lookup("VirtualFlow") as VirtualFlow<*>
        }
        this.listView.addEventFilter(ScrollEvent.SCROLL) {
            decideLoadMore()
        }

        this.listView.setCellFactory {
            return@setCellFactory getTextItem()
        }

        this.listView.selectionModel.selectedItemProperty().addListener(DataChangeListener { itItem ->
            onItemClick(itItem)
        })
        //this.virtualFlow.
    }


    protected open fun getListViewId(): String {
        return "#listView"
    }

    protected abstract fun getTextItem(): ListCell<T>

    protected open fun onItemClick(item: T?) {
        Platform.runLater {
            this.listView.selectionModel.clearSelection()
        }
        item?.let {
            onAdapterItemClickListener?.onItemClick(item)
        }
    }

    override fun refresh(list: MutableList<*>?) {
        super.refresh(list)
        this.listView.items.clear()
    }

    override fun loadMoreComplete() {
        super.loadMoreComplete()
        this.listView.items.setAll(vmBox.dataList)
        Single.timer(100, TimeUnit.MILLISECONDS)
            .compose(SlcRxJavaUtils.applyOsAndroidSchedulers())
            .subscribe(object : SimpleSingleObserver<Long>() {
                override fun onSuccess(t: Long) {
                    super.onSuccess(t)
                    //没有找到listView刷新完成方法。所以此处用延迟来检测，不准确，但没办法。
                    decideLoadMore()
                }
            })
    }

    override fun loadMoreEnd() {
        super.loadMoreEnd()
        this.listView.items.setAll(vmBox.dataList)
    }

    /**
     * 决定刷新方法
     */
    protected fun decideLoadMore() {
        if (listPageWrapper.loadMoreDataFormat3.canLoadMoreEnd()) {
            Platform.runLater {
                //Log.get().info("VirtualFlow.position：{}", this@BaseListPageViewVd.virtualFlow.position)
                var lastCallShow = false
                if (this.virtualFlow.cellCount > 0) {
                    lastCallShow =
                        this.virtualFlow.lastVisibleCell.equals(this.virtualFlow.getCell(this.virtualFlow.cellCount - 1))
                }
                if (this.virtualFlow.position > 0.9 || lastCallShow) {
                    vmBox.loadMore()
                }
            }
        }
    }

}
